 /* eslint-disable */
if (typeof getComputedStyle === 'undefined') {
  if(document.body.currentStyle) {
    window.getComputedStyle = function (el) {
      return el.currentStyle;
    }
  } else {
    window.getComputedStyle = {};
  }
} 
function getOuterW(dom) {
  const style = getComputedStyle(dom);
  let w = 0;
  if (style) {
    w = parseFloat(style.paddingLeft) +
      parseFloat(style.paddingRight) +
      parseFloat(style.borderLeftWidth) +
      parseFloat(style.borderRightWidth);
  }
  return w;
}

function getOuterH(dom) {
  const style = getComputedStyle(dom)
  let h = 0;
  if (style) {
    h = parseFloat(style.paddingTop) +
      parseFloat(style.paddingBottom) +
      parseFloat(style.borderTopWidth) +
      parseFloat(style.borderBottomWidth);
  }
  return h;
}

const getQuery = function (name) {
  let u = arguments[1] || window.location.search,
    reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'),
    r = u.substr(u.indexOf('?') + 1).match(reg);
  return r != null ? r[2] : '';
};

function __getVideoWh(dom) {
  let max_width = getMaxWith(),
    width = max_width,
    ratio_ = dom.getAttribute('data-ratio') * 1 || (4 / 3),
    arr = [4 / 3, 16 / 9],
    ret = arr[0],
    abs = Math.abs(ret - ratio_);
  for (let j = 1, jl = arr.length; j < jl; j++) {
    const _abs = Math.abs(arr[j] - ratio_);
    if (_abs < abs) {
      abs = _abs;
      ret = arr[j];
    }
  }
  ratio_ = ret;
  let parent_width = getParentWidth(dom) || max_width,
    outerW = getOuterW(dom) || 0,
    outerH = getOuterH(dom) || 0,
    videoW = width - outerW,
    videoH = videoW / ratio_,
    height = videoH + outerH;

  width = width > parent_width ? parent_width : width;
  return { w: width, h: height, vh: videoH, vw: videoW, ratio: ratio_ };
}


function getMaxWith() {
  return window.innerWidth - 30;
}


function getParentWidth(dom) {
  let parent_width = 0;
  let parent = dom.parentNode;
  let outerWidth = 0;
  while (true) {
    if (!parent || parent.nodeType !== 1) { break }
    const parent_style = getComputedStyle(parent);
    if (!parent_style) { break }
    parent_width = parent.clientWidth - parseFloat(parent_style.paddingLeft) -
      parseFloat(parent_style.paddingRight) - outerWidth;
    if (parent_width > 0) { break }
    outerWidth += parseFloat(parent_style.paddingLeft) +
      parseFloat(parent_style.paddingRight) +
      parseFloat(parent_style.marginLeft) +
      parseFloat(parent_style.marginRight) +
      parseFloat(parent_style.borderLeftWidth) +
      parseFloat(parent_style.borderRightWidth);
    parent = parent.parentNode;
  }
  return parent_width;
} 
function setVideoSrc() {

  const iframe = document.getElementsByTagName('iframe');
  if(iframe.length){
  //  Toast.loading('正在加载中...');
    for (let i = 0, il = iframe.length; i < il; i++) {
      const a = iframe[i];
      const src_ = a.getAttribute('src') || a.getAttribute('data-src') || '';
      if (!/http(s)*:\/\/v\.qq\.com\/iframe\/(preview|player)\.html\?/.test(src_)) {
        //console.log(iframe[i].parentNode)
        iframe[i].parentNode.style= 'display:none !important;'
        continue;
      }
      let vid = getQuery('vid', src_);
      if (!vid) {
        continue;
      }

      vid = vid.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
      a.removeAttribute('src');
      const obj = __getVideoWh(a);
      a.style.cssText += ';width: ' + obj.w + 'px !important;';
      a.setAttribute('width', '100%');
      a.style.cssText += 'height: ' + obj.h + 'px !important;';
      a.setAttribute('height', '100%');
      a.setAttribute('id', 'MyIframe'+i);
      a.setAttribute('name', 'MyIframe');
      a.setAttribute('data-vh', obj.vh);
      a.setAttribute('data-vw', obj.vw);
      //a.setAttribute('allowfullscreen',false);
      const src = window.location.protocol + '//v.qq.com/iframe/player.html?vid=' + vid + '&width=' + obj.vw + '&height=' + obj.vh + '&auto=0';
 
      // new 删除旧的iframe 新建iframe，避免浏览器history被iframe污染
      const newIframe = a.cloneNode();
      newIframe.setAttribute('src', src);
      const parentElement = a.parentElement;
      parentElement.removeChild(a);
      parentElement.appendChild(newIframe);
    }
    //Toast.hide();
  }
}

// window.setVideoSrc = setVideoSrc;
export default {
  install(){
    setVideoSrc()
  }
} ;